package com.meila.cms.util;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

import com.meila.cms.dao.DictMapper;
import com.meila.cms.model.CommonModel;
import com.meila.cms.model.SQLAdapterModel;

@Component
public class DictUtil {
	
	private  DictMapper dictMapper;
	
	
	public DictMapper getDictMapper() {
		return dictMapper;
	}
	@Inject
	public void setDictMapper(DictMapper dictMapper) {
		this.dictMapper = dictMapper;
	}

	public  String getNameByValue(String category,String value) {
		 
		String name = null;
		SQLAdapterModel sqlAdapterModel=new SQLAdapterModel("select distinct type from t_dict where category='"+category+"'");
		String type=dictMapper.selectString(sqlAdapterModel);
		List<CommonModel> list = null;
		
        if("0".equals(type)){

			// 查询字典数据字典配置的是常量
			sqlAdapterModel.setSql("select name from t_dict where category ='"+ category + "' and value='"+value+"'");
			name = dictMapper.selectString(sqlAdapterModel);

			
		}
		
		if("1".equals(type)){
			

			// 查询字典数据字典配置的sql
			sqlAdapterModel.setSql("select sqls from t_dict where category ='" + category + "'");
			String sqls = dictMapper.selectString(sqlAdapterModel);
			sqlAdapterModel.setSql("select text from ("+sqls+") a where value='"+value+"'");
			name = dictMapper.selectString(sqlAdapterModel);
	 
		}
		
		return name; 
   }
 
 
}
